package com.myh.cc.coom.search.viewmodel

import android.content.Intent
import android.view.View
import com.blankj.utilcode.utils.LogUtils.i
import com.myh.cc.base.BaseModel
import com.myh.cc.coom.main.view.PlayActivity
import com.myh.cc.coom.search.adapter.HotAdapter
import com.myh.cc.coom.search.adapter.SearchAdapter
import com.myh.cc.coom.search.bean.HotBean
import com.myh.cc.coom.search.bean.SearchBean
import com.myh.cc.coom.search.net.SearchNetControl
import com.myh.cc.coom.search.view.SearchFragment
import com.myh.cc.databinding.FragmentSearchBinding
import com.myh.cc.util.ConstantUtil
import com.myh.cc.widget.OnItemClickListener

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/15.   ：
 */
class SearchModel: BaseModel<FragmentSearchBinding,SearchNetControl>(),OnItemClickListener {
    var  bean:SearchBean? = null
    lateinit var  fragment: SearchFragment

    override fun onCreate() {
        super.onCreate()
        fragment = UI as SearchFragment
    }

    override fun onBeforeRequest(tag: Int) {
    }

    override fun onSuccess(data: Any, tag: Int) {

        when(tag){
            ConstantUtil.requestTagOne -> initHot(data as HotBean)
            ConstantUtil.requestTagTwo -> {
                bean  = data as SearchBean
                initSearch(bean!!)
            }

        }

    }

    override fun onError(errorMag: String, tag: Int) {
        i(errorMag)
    }

    /**
     * 获取热搜
     */
    fun getHot(){

        var map = HashMap<String,String>()
        map.put("format","json")

        mControl.getHot(this,"",map,ConstantUtil.requestTagOne)
    }

    /**
     *      参数：

            w：搜索关键字
            p：当前页
            n：每页歌曲数量
     */
    fun getSearch(w: String){

        var map = HashMap<String,String>()
        map.put("format","json")
        map.put("needNewCode","1")
        map.put("w",w)
        map.put("n","20")
        map.put("p","1")


        mControl.getSearch(this,"",map,ConstantUtil.requestTagTwo)
    }


    private fun initHot(bean: HotBean){

        mBinder.searchRvHot.adapter = HotAdapter(bean,this,mBinder)
        changeRecycleView(1)
    }

    private fun initSearch(bean: SearchBean){
        val  adapter = SearchAdapter(bean)
        mBinder.searchRvResult.adapter = adapter
        changeRecycleView(2)

        adapter.mListenre = this
    }

    /**
     * 改变 控件显隐
     * 1 hot 显示  search 隐藏
     * 2 search 显示 hot 隐藏
     */
    fun changeRecycleView(type: Int){

        when(type){
            1 -> {
                mBinder.searchRvResult.visibility = View.GONE
                mBinder.earchLinearHot.visibility = View.VISIBLE
            }
            2 -> {
                mBinder.searchRvResult.visibility = View.VISIBLE
                mBinder.earchLinearHot.visibility = View.GONE
            }
        }
    }

    override fun onClick(view: View, position: Int) {
        val intent = Intent(getContext(),PlayActivity::class.java)
        intent.putExtra("mid",bean!!.data.song.list[position].songmid)

        fragment.startActivity(intent)
    }

    override fun onItemLongClick(view: View, position: Int) {


    }
}